package com.supreme.omsserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.omsserver.component.BloomFilterService;
import com.supreme.omsserver.entity.mysql.Store;
import com.supreme.omsserver.service.orm.StoreOrmService;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "store")
public class StoreController {

    @Resource
    private StoreOrmService storeOrmService;

    @Resource
    private BloomFilterService bloomFilterService;

    @Resource
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @GetMapping(value = "list/{key}")
    public AjaxResp storeList(@PathVariable(value = "key") String key) {
        List<Store> storeList = storeOrmService.list();
        RedisTemplate<String, Serializable> redisTemplate = redisTemplateList.get(0);

        ValueOperations<String, Serializable> valueOperations = redisTemplate.opsForValue();
        storeList.forEach(store -> valueOperations.set("STORE:" + store.getStoreCode(), store));

        Store[] storeArray = storeList.toArray(new Store[0]);
        ListOperations<String, Serializable> listOperations = redisTemplate.opsForList();
        redisTemplate.delete("STORE_LIST");
        listOperations.leftPushAll("STORE_LIST", storeArray);

        Map<String, Store> storeMap = storeList.stream().collect(Collectors.toMap(Store::getStoreCode, Function.identity()));
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        redisTemplate.delete("STORE_HASH");
        hashOperations.putAll("STORE_HASH", storeMap);

        List<String> storeCodeList = storeList.stream()
                .map(Store::getStoreCode)
                .collect(Collectors.toList());

        RBloomFilter<Object> bloomFilter = bloomFilterService.getBloomFilter(key, 10000, 0.00001);
        storeCodeList.forEach(bloomFilter::add);

        return AjaxResp.instance(storeCodeList);
    }

    @GetMapping(value = "exist/{key}/{storeCode}")
    public AjaxResp exist(@PathVariable(value = "key") String key, @PathVariable(value = "storeCode") String storeCode) {
        RBloomFilter<Object> bloomFilter = bloomFilterService.getBloomFilter(key, 10000, 0.00001);
        boolean contains = bloomFilter.contains(storeCode);
        return AjaxResp.instance(contains);
    }
}
